package com.my.api.controller;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.my.api.request.AppRequestHead;
import com.my.biz.service.IGroupInfoService;
import com.my.biz.service.IGroupMemberInfoService;
import com.my.util.JWTUtil;
import com.my.util.MsgUtil;

@Controller
@RequestMapping(value = "v1/group", produces = {"application/json;charset=UTF-8"})
public class GroupController {
	
	@Autowired
	private IGroupInfoService groupService;
	
	@Autowired
	private IGroupMemberInfoService groupMemberInfoService;
	
	@RequestMapping(value = "groups")
	@ResponseBody
	public Object getGroups(AppRequestHead header) {
		return groupService.getGroupInfosByUid(JWTUtil.getUserIdByToken(header.getToken()));
	}
	
	
	@RequestMapping(value = "{group}/members")
	@ResponseBody
	public Object getMembers(AppRequestHead header,@PathVariable("group")String group,@RequestParam("next")String next) throws Exception{
		if(StringUtils.isBlank(group)){
			return MsgUtil.error(400, "非法参数");
		}
		return groupMemberInfoService.getMemberByUidAndGroup(JWTUtil.getUserIdByToken(header.getToken()), group, next);
	}
	
	@RequestMapping(value = "add")
	@ResponseBody
	public Object add(AppRequestHead header,@RequestParam String qq_group) {
		
		return groupService.addGroupByUid(JWTUtil.getUserIdByToken(header.getToken()), qq_group);
	}
}
